package com.client;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.visualization.client.DataTable;

public class PLCharts extends SlidePanel {

	private DecoratorPanel dplCharts;
	private DPCharts dpCharts;
	private FlexTable ftFooter;
	private Button btnBack;
	private ListBox lbChType;

	private DataTable data = null;

	protected void onFirstShow() {
		dplCharts = new DecoratorPanel();
		dpCharts = new DPCharts();
		dpCharts.setAnimationEnabled(true);

		ftFooter = new FlexTable();
		btnBack = new Button(CNStatsFront.get().constants.backToSearch(),
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						CNStatsFront.get().plSearch.reset();
						CNStatsFront.get().plBody.showWidget(0);
					}
				});
		lbChType = new ListBox(false);
		lbChType.addItem(CNStatsFront.get().constants.areaChart(), "Area");
		lbChType.addItem(CNStatsFront.get().constants.barChart(), "Bar");
		lbChType.addItem(CNStatsFront.get().constants.columnChart(), "Column");
		lbChType.addItem(CNStatsFront.get().constants.lineChart(), "Line");
		lbChType.addItem(CNStatsFront.get().constants.pieChart(), "Pie");
		lbChType.addItem(CNStatsFront.get().constants.tableChart(), "Table");
		lbChType.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				String type = lbChType.getValue(lbChType.getSelectedIndex());
				if (type.equals("Area"))
					dpCharts.drawAreaChart(data);
				else if (type.equals("Bar"))
					dpCharts.drawBarChart(data);
				else if (type.equals("Column"))
					dpCharts.drawColumnChart(data);
				else if (type.equals("Line"))
					dpCharts.drawLineChart(data);
				else if (type.equals("Pie"))
					dpCharts.drawPieChart(data);
				else if (type.equals("Table"))
					dpCharts.drawTableChart(data);
			}

		});

		this.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		dplCharts.add(dpCharts);
		this.add(dplCharts);
		this.setCellVerticalAlignment(dplCharts, HasVerticalAlignment.ALIGN_MIDDLE);

		this.add(ftFooter);
		ftFooter.setWidget(0, 0, btnBack);
		ftFooter.setHTML(0, 1, CNStatsFront.get().constants.chartType());
		ftFooter.setWidget(0, 2, lbChType);
		this.setCellVerticalAlignment(ftFooter, HasVerticalAlignment.ALIGN_TOP);
	}

	public void setData(DataTable data) {
		this.data = data;
	}

	public void draw() {
		dpCharts.drawLineChart(data);
		lbChType.setSelectedIndex(3);
	}

}
